package com.MinesweeperGame.server;

import java.util.Iterator;
import java.util.List;

import javax.jdo.Extent;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import com.MinesweeperGame.client.GameInfo;
import com.MinesweeperGame.client.GameService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;


@SuppressWarnings("serial")
public class GameServiceImpl extends RemoteServiceServlet implements GameService {
	
	

	public String addToScore(int timeTaken) {
		
		PersistenceManager pm = LocalPersistanceManager.getPersistenceManager();

		    try {
		      pm.makePersistent(new GameTime(timeTaken));
		      pm.flush();
		      System.out.println(""+timeTaken);
		      System.out.println("Data has been inserted");
		    } finally {
		      pm.close();
		    }
			return null ;
			
		
	}

	public List<GameInfo> getScore() {
		
		PersistenceManager pm = LocalPersistanceManager.getPersistenceManager();

		
		Extent   extent = pm.getExtent (GameTime.class,true);
        Iterator    it  = extent.iterator();
        
        
        System.out.print("List of Scores");
        while(it.hasNext())
        {
            GameTime time = (GameTime) it.next();
            int scores = time.getTimeTaken() ;
           System.out.println (scores);
        }
		return null ;
	}
	
	public List<GameInfo> getHighScores() {
		
		PersistenceManager pm = LocalPersistanceManager.getPersistenceManager();

		
		Extent   extent = pm.getExtent (GameTime.class,true);
        Iterator    it  = extent.iterator();
        int highScore = 0;
        
    
        while(it.hasNext())
        {
            GameTime time = (GameTime) it.next();
            int temp = time.getTimeTaken() ;
            if(temp > highScore) {
            	highScore = temp;
            }
           
        }
        System.out.println ("High Score : " + highScore);
		return null;
	}
	
//	private PersistenceManager getPersistenceManager() {
//	    return PMF.getPersistenceManager();
//	}
}
